package org.dromara.wx.service.impl;

import org.dromara.common.core.service.wx.WxUserRoleTranslationService;
import org.springframework.cache.annotation.Cacheable;
import org.dromara.common.core.utils.MapstructUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.dromara.wx.domain.WxUserRole;
import org.dromara.wx.domain.bo.WxUserRoleBo;
import org.dromara.wx.domain.query.WxUserRoleQuery;
import org.dromara.wx.domain.vo.WxUserRoleVo;
import org.dromara.wx.mapper.WxUserRoleMapper;
import org.dromara.wx.service.IWxUserRoleService;

import java.util.Collection;
import java.util.List;

/**
 * 用户和角色关联Service业务层处理
 *
 * @author yf
 * @date 2024-06-05
 */
@Service
public class WxUserRoleServiceImpl extends ServiceImpl<WxUserRoleMapper, WxUserRole> implements IWxUserRoleService, WxUserRoleTranslationService {

    /**
     * 查询用户和角色关联
     *
     * @param userRoleId 主键
     * @return WxUserRoleVo
     */
    @Override
    public WxUserRoleVo queryById(Integer userRoleId) {
        return baseMapper.selectVoById(userRoleId);
    }

    /**
     * 查询用户和角色关联列表
     *
     * @param query 查询对象
     * @return WxUserRoleVo
     */
    @Override
    public TableDataInfo<WxUserRoleVo> queryPageList(WxUserRoleQuery query) {
        return PageQuery.of(() -> baseMapper.queryList(query));
    }

    /**
     * 查询用户和角色关联列表
     *
     * @param query 查询对象
     * @return WxUserRoleVo
     */
    @Override
    public List<WxUserRoleVo> queryList(WxUserRoleQuery query) {
        return baseMapper.queryList(query);
    }

    /**
     * 根据新增业务对象插入用户和角色关联
     *
     * @param bo 用户和角色关联新增业务对象
     * @return Boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(WxUserRoleBo bo) {
        WxUserRole add = MapstructUtils.convert(bo, WxUserRole.class);
        return save(add);
    }

    /**
     * 根据编辑业务对象修改用户和角色关联
     *
     * @param bo 用户和角色关联编辑业务对象
     * @return Boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(WxUserRoleBo bo) {
        WxUserRole update = MapstructUtils.convert(bo, WxUserRole.class);
        return updateById(update);
    }

    /**
     * 校验并批量删除用户和角色关联信息
     *
     * @param ids 主键集合
     * @return Boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Integer> ids) {
        return removeByIds(ids);
    }

    /**
     * 翻译用户和角色关联信息
     *
     * @param id 主键
     * @return WxUserRoleVo
     */
    @Override
    @Cacheable(cacheNames = WxUserRole.TRANS_ID_TO_OBJECT, key = "#id")
    public Object selectObjectById(Integer id){
        return baseMapper.selectVoById(id);
    }
}
